// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { CoreError } from '@classes/errors/error';

/**
 * Error returned by WS.
 */
export class CoreWSError extends CoreError {

    exception?: string; // Name of the Moodle exception.
    errorcode?: string;
    warningcode?: string;
    link?: string; // Link to the site.
    moreinfourl?: string; // Link to a page with more info.
    debuginfo?: string; // Debug info. Only if debug mode is enabled.
    backtrace?: string; // Backtrace. Only if debug mode is enabled.

    constructor(error: CoreWSErrorData) {
        super(error.message);

        this.exception = error.exception;
        this.errorcode = error.errorcode;
        this.warningcode = error.warningcode;
        this.link = error.link;
        this.moreinfourl = error.moreinfourl;
        this.debuginfo = error.debuginfo;
        this.backtrace = error.backtrace;
    }

    /**
     * Given an error returned by a WS call, check if the error is generated by the app or it has been returned by the WebService.
     *
     * @param error Error to check.
     * @returns Whether the error was returned by the WebService.
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    static isWebServiceError(error: any): boolean {
        return !!error && (
            error.warningcode !== undefined ||
            (
                error.errorcode !== undefined && error.errorcode != 'userdeleted' && error.errorcode != 'upgraderunning' &&
                error.errorcode != 'forcepasswordchangenotice' && error.errorcode != 'usernotfullysetup' &&
                error.errorcode != 'sitepolicynotagreed' && error.errorcode != 'sitemaintenance' &&
                error.errorcode != 'wsaccessusersuspended' && error.errorcode != 'wsaccessuserdeleted' &&
                !this.isExpiredTokenError(error)
            ) ||
            !!error.status && error.status >= 400 // CoreHttpError, assume status 400 and above are like WebService errors.
        );
    }

    /**
     * Given an error returned by a WS call, check if the error is a token expired error.
     *
     * @param error Error to check.
     * @returns Whether the error is a token expired error.
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    static isExpiredTokenError(error: any): boolean {
        return error.errorcode === 'invalidtoken' ||
            (error.errorcode === 'accessexception' && error.message.includes('Invalid token - token expired'));
    }

}

type CoreWSErrorData = {
    message?: string;
    exception?: string; // Name of the Moodle exception.
    errorcode?: string;
    warningcode?: string;
    link?: string; // Link to the site.
    moreinfourl?: string; // Link to a page with more info.
    debuginfo?: string; // Debug info. Only if debug mode is enabled.
    backtrace?: string; // Backtrace. Only if debug mode is enabled.
};
